/**
 * 
 */
package com.xbang.attendance.security.support;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.security.userdetails.UserDetails;
import org.springframework.security.userdetails.UserDetailsService;
import org.springframework.security.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.xbang.attendance.dao.SystemDao;
import com.xbang.attendance.entity.role.Resource;
import com.xbang.attendance.entity.role.User;
import com.xbang.attendance.entity.system.Menu;
import com.xbang.attendance.utils.ComparatorMenu;
import com.xbang.core.dao.annotation.Dynamic;

import edu.emory.mathcs.backport.java.util.Collections;
import enumeration.ResourceType;

/**
 * @author dev.zhang
 * @author Downpour
 */
@Service("securityManager")
public class SecurityManagerSupport implements UserDetailsService, com.xbang.attendance.security.SecurityManager {

	@Dynamic
	private SystemDao systemDao;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.security.userdetails.UserDetailsService#loadUserByUsername(java.lang.String)
	 */
	public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException, DataAccessException {
		List<User> users = systemDao.getUserByName(userName);
		if (users.isEmpty()) {
			throw new UsernameNotFoundException("User " + userName + " has no GrantedAuthority");
		}
		return users.get(0);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.demo2do.yoda.security.SecurityManager#listMenus()
	 */
	public List<Menu> listMenus() {
		return systemDao.listMenus();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.demo2do.yoda.security.SecurityManager#listSubmenus()
	 */
	public Map<Integer, List<Menu>> listSubmenus() {
		ComparatorMenu comparatorMenu = new ComparatorMenu();
		Map<Integer, List<Menu>> submenuMap = new HashMap<Integer, List<Menu>>();
		List<Menu> submenus = systemDao.listSubmenus();
		for (Menu submenu : submenus) {
			// already contains a key, then add to it
			if (submenuMap.containsKey(submenu.getParent().getId())) {
				List<Menu> tmpSubMList = submenuMap.get(submenu.getParent().getId());
				tmpSubMList.add(submenu);
				Collections.sort(tmpSubMList, comparatorMenu);
				submenuMap.put(submenu.getParent().getId(), tmpSubMList);
			} else {
				// key do not exist, so create a new one
				List<Menu> temp = new ArrayList<Menu>();
				temp.add(submenu);
				submenuMap.put(submenu.getParent().getId(), temp);
			}
		}
		return submenuMap;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.demo2do.yoda.security.SecurityManager#listUrlAuthorities()
	 */
	public Map<String, String> listUrlAuthorities() {
		Map<String, String> urlAuthorities = new HashMap<String, String>();
		List<Resource> urlResources = systemDao.listUrlResources(ResourceType.URL);
		for (Resource resource : urlResources) {
			urlAuthorities.put(resource.getValue(), resource.getRoleAuthorities());
		}
		return urlAuthorities;
	}

}
